package com.service.Authorization.controller;

import com.service.Authorization.dto.UserDto;
import com.service.Authorization.entity.QyProgrammeApiBind;
import com.service.Authorization.service.QyCorpInfoService;
import com.service.Authorization.vo.*;
import com.service.config.annotation.LoginUser;
import com.service.config.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 企业微信交互接口
 */
@RestController
@RequestMapping("QyWx")
@Api(value = "企业微信交互接口", tags = "企业微信交互接口")
public class QyWxController {
    /**
     * 服务对象
     */
    @Resource
    private QyCorpInfoService qyCorpInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增企业信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "addQyCorpInfo",produces="application/json")
    @ApiOperation(value = "新增企业信息")
    @ResponseBody
    public Result addQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                @RequestBody AddQyCorpInfoDto addQyCorpInfoDto) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return qyCorpInfoService.addQyCorpInfo(addQyCorpInfoDto,userDto);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 删除企业信息
     *
     * @param
     * @return
     */
    @GetMapping(value = "deleteQyCorpInfo",produces="application/json")
    @ApiOperation(value = "删除企业信息")
    @ResponseBody
    public Result deleteQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                   @RequestParam @ApiParam("企业信息id") String qyCorpInfoId) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.deleteQyCorpInfo(userId,qyCorpInfoId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 更新企业信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "updateQyCorpInfo",produces="application/json")
    @ApiOperation(value = "更新企业信息")
    @ResponseBody
    public Result updateQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                   @RequestBody UpdateQyCorpInfoDto updateQyCorpInfoDto) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.updateQyCorpInfo(updateQyCorpInfoDto);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 刷新url页面时拼接的参数
     *
     * @param
     * @return
     */
    @GetMapping(value = "freshQyCorpInfo",produces="application/json")
    @ApiOperation(value = "刷新url页面时拼接的参数")
    @ResponseBody
    public Result freshQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                   @RequestParam String qyCorpInfoId) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.freshQyCorpInfo(qyCorpInfoId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 获取企业信息列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "getQyCorpInfoList",produces="application/json")
    @ApiOperation(value = "获取企业信息列表")
    @ResponseBody
    public Result getUserInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                              @RequestParam(value = "type",required = false) @ApiParam("0--企业微信 1--微信小程序") Integer type) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.queryByUserId(userId,type);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 企业绑定方案和api
     *
     * @param
     * @return
     */
    @PostMapping(value = "QyCorpInfoBindApi",produces="application/json")
    @ApiOperation(value = "企业绑定方案和api")
    @ResponseBody
    public Result QyCorpInfoBindApi(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                    @RequestBody BindAccount bindAccount) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                List<QyProgrammeApiBind> qyProgrammeApiBindList = ModelMapperUtil.strictMapList(bindAccount.getBindAccountList(), QyProgrammeApiBind.class);
                return qyCorpInfoService.QyCorpInfoBindApi(qyProgrammeApiBindList,userId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 企业绑定方案和api
     *
     * @param
     * @return
     */
    @PostMapping(value = "FindQyCorpInfoBindApi",produces="application/json")
    @ApiOperation(value = "查询企业绑定方案和api")
    @ResponseBody
    public Result FindQyCorpInfoBindApi(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                    @RequestParam @ApiParam("方案id") String programmeId) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.FindQyCorpInfoBindApi(programmeId,userId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 企业解除绑定方案和api
     *
     * @param
     * @return
     */
    @PostMapping(value = "cancelQyCorpInfoBindApi",produces="application/json")
    @ApiOperation(value = "企业解除绑定方案和api")
    @ResponseBody
    public Result cancelQyCorpInfoBindApi(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                    @RequestBody QyCorpInfoVo qyCorpInfoVo) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.cancelQyCorpInfoBindApi(qyCorpInfoVo);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 获取企业详情已经绑定的方案和api
     *
     * @param
     * @return
     */
    @GetMapping(value = "getQyCorpInfoDetails",produces="application/json")
    @ApiOperation(value = "获取方案绑定的方案和api以及url")
    @ResponseBody
    public Result getQyCorpInfoDetails(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,@LoginUser String userId,
                                       @RequestParam @ApiParam("方案id") String programmeId) throws Exception {
        if (StringUtils.isNotEmpty(userId)) {
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return qyCorpInfoService.getQyCorpInfoDetails(userId,programmeId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }

    /**
     * 微信获取html
     *
     * @param
     * @return
     */
    @PostMapping(value = "getHtml",produces="application/json")
    @ApiOperation(value = "portal页面获取")
    @ResponseBody
    public Result getHtml(@RequestBody AuthorizationVo authorizationVo) throws Exception {
        return qyCorpInfoService.getHtml(authorizationVo);
    }
    /**
     * 微信获取html
     *
     * @param
     * @return
     */
    @GetMapping(value = "getWeiChatHtml",produces="application/json")
    @ApiOperation(value = "微信portal页面获取")
    @ResponseBody
    public Result getWeiChatHtml(@RequestParam @ApiParam("number小程序的url所需参数") String number,
                                 @RequestParam  @ApiParam("programmeId 方案id")  String programmeId) throws Exception {
        return qyCorpInfoService.getWeiChatHtml(number,programmeId);
    }
    /**
     * 企业解除绑定方案和api
     *
     * @param
     * @return
     */
    @PostMapping(value = "checkQyWeiUser",produces="application/json")
    @ApiOperation(value = "微信用户校验身份")
    @ResponseBody
    public Result checkQyWeiUser(@RequestBody SecretAndCode secretAndCode,
                                 @RequestHeader(value = "token",required = false) @ApiParam("token令牌") String bAuthToken) throws Exception {
        if (secretAndCode.getCode() == null ||"".equals(secretAndCode.getCode()) || secretAndCode.getSecret() == null || "".equals(secretAndCode.getSecret())) {
            return Result.failure(PARAMETER_NOT_FOUND,parameterNotFound);
        }
        return qyCorpInfoService.checkQyWeiUser(bAuthToken,secretAndCode);
    }
    /**
     * 微信小程序校验身份
     *
     * @param
     * @return
     */
    @PostMapping(value = "checkQyWeChatUser",produces="application/json")
    @ApiOperation(value = "微信小程序校验身份")
    @ResponseBody
    public Result checkQyWeChatUser(@RequestBody AuthorizationVo authorizationVo,
                                 @RequestHeader(value = "token",required = false) @ApiParam("token令牌") String bAuthToken) throws Exception {
        if (authorizationVo.getCode() == null ||"".equals(authorizationVo.getCode())
                || authorizationVo.getPortalUserId() == null ||"".equals(authorizationVo.getPortalUserId())
                || authorizationVo.getProgrammeInfoId() == null ||"".equals(authorizationVo.getProgrammeInfoId())
                ||authorizationVo.getApmac() == null ||"".equals(authorizationVo.getApmac())
                ||authorizationVo.getSsid() == null ||"".equals(authorizationVo.getSsid())) {//校验参数
            return Result.failure(PARAMETER_NOT_FOUND,parameterNotFound);
        }
        if (authorizationVo.getPortalUserId().length() !=16 || authorizationVo.getProgrammeInfoId().length() != 16) {
            return Result.failure(PARAMETER_NOT_FOUND,parameterNotFound);
        }
        return qyCorpInfoService.checkQyWeChatUser(bAuthToken,authorizationVo);
    }
    /**
     * 企业解除绑定方案和api
     *
     * @param
     * @return
     */
    @GetMapping(value = "checkQyWeiUserResult",produces="application/json")
    @ApiOperation(value = "页面校验认证结果")
    @ResponseBody
    public Result checkQyWeiUserResult( @RequestParam String apmac,
                                  @RequestParam String umac,
                                  @RequestParam String ssid) throws Exception {
        return qyCorpInfoService.checkQyWeiUser(apmac,umac,ssid);
    }
    /**
     *  获取小程序 scheme 码
     *
     * @param
     * @return
     */
    @PostMapping(value = "getUrlScheme",produces="application/json")
    @ApiOperation(value = " 获取小程序cheme码")
    @ResponseBody
    public Result getUrlScheme(@RequestBody AuthorizationVo authorizationVo) throws Exception {
        return qyCorpInfoService.getUrlScheme(authorizationVo);
    }
}
